/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.dao.impl;

import com.rbh.risk.dao.ProfileDao;
import com.rbh.risk.dao.RoleDao;
import com.rbh.risk.entity.Opduser;
import com.rbh.risk.entity.Profile;
import com.rbh.risk.support.SessionFactorySupport;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("profileDao")
public class ProfileDaoImpl extends SessionFactorySupport implements ProfileDao {

    @Resource(name = "roleDao")
    private RoleDao roleDao;

    @Override
    public void insert(Profile profile) throws DataAccessException {
        try {
            profile.setRole(roleDao.getDefaultRole());
            super.getSessionFactory().getCurrentSession().saveOrUpdate(profile);
        } catch (DataAccessException | HibernateException ex) {
            throw ex;
        }
    }

    @Override
    public void update(Profile profile) throws DataAccessException {
        try {
            profile.setProfileLastAccessDatetime(profile.getProfileAccessDatetime());
            profile.setProfileAccessDatetime(new Date());
            super.getSessionFactory().getCurrentSession().update(profile);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void delete(Profile profile) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().delete(profile);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public Profile getById(String profileId) throws DataAccessException {
        Profile result = null;
        try {
            result = (Profile) super.getSessionFactory().getCurrentSession().get(Profile.class, profileId);
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public List<Profile> getAll() throws DataAccessException {
        List<Profile> results = null;
        try {
            Query query = super.getSessionFactory().getCurrentSession().createQuery("from " + Profile.class.getName());
            results = query.list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public Profile insertAutomatic(Profile profile) throws DataAccessException {
        Profile result = null;
        try {
            profile.setRole(roleDao.getDefaultRole());
            Serializable id = super.getSessionFactory().getCurrentSession().save(profile);
            result = (Profile) super.getSessionFactory().getCurrentSession().get(Profile.class, id);
        } catch (DataAccessException | HibernateException ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public Profile getByOpduser(Opduser opduser) throws DataAccessException {
        Profile result = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(Profile.class);
            criteria.setFetchMode("workgroupList", FetchMode.SELECT);
            criteria.add(Restrictions.eq("opduser", opduser));
            result = (Profile) criteria.uniqueResult();

            if (result != null) {
                this.update(result);
            } else {
                Profile profile = new Profile();
                profile.setProfileStatus(true);
                profile.setOpduser(opduser);
                result = this.insertAutomatic(profile);
            }
        } catch (HibernateException | DataAccessException ex) {
            throw ex;
        }
        return result;
    }

    public RoleDao getRoleDao() {
        return roleDao;
    }

    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }
}
